﻿/*
创建人：longb
日期：2022-06-16
描述：事务管理js
*/
var page = {
    pagetotal: 0,
    pageSize: 20,
    pageNumber: 1,
    mainEmplID:null,
    pageType: getParam("type"), //0：创建 1：审核
    rootPath: getRootPath(), //网站根路径
    ajaxUrl: getRootPath() + "/Ashx/ZsHandler/ZSTransactionHandler.ashx",
    //初始化页面
    init: function () {
        page.bind();
        customizedEasyuiPagination('pp'); //定制化分页
        if (page.pageType == "0") {
            $("#SaveCreate").css("display", "");
        }
        if (page.pageType == "1") {
            $("#Recieve").css("display", "");
            $('.EditorpanelSection').panel("setTitle","查看区域");
            page.DisableForm();
        }
        page.initComboxtree();
    },
    DisableForm:function(){//表单编辑框禁止编辑
    },
    //页面事件绑定
    bind: function () {
        var data = [{ id:0, text: '待接收' }, { id:1, text: '进行中' }, { id:2, text: '已完成' },{ id:3, text: '未完成' }];
        $('#Status').combobox({
            data: data,
            valueField: 'id',
            textField: 'text',
            value:0
        });
        $('#TransactionType,#TransactionType1').combobox({
            data: [{ id:"0", text: '领导交办' }, { id:"1", text: '会议纪要' },{ id:"3", text: '领导班子会' },
            { id:"4", text: '党会' },{ id:"5", text: '每周班子碰头会' },{ id:"6", text: '文件' },
            { id:"7", text: '2023年重点工作' }, { id:"2", text: '其他'}],
            valueField: 'id',
            textField: 'text'
        });
       
        $('#TRANSACTIONCATEGORY1').combobox({
            data:[{id:"0", text: '技术' },{id:"1", text: '质量' },{id:"2", text: '生产交付' },{id:"3", text: 'AEOS' },{id:"4", text: '保密管理' },{id:"5", text: '成本财务' },
            {id:"6", text: '党建' },{id:"7", text: '行政' },{id:"8", text: '其他' }
            ],
            valueField: 'id',
            textField: 'text'
        });
//        $('#MAINEMPLOYEE').newselectCombox({},{},{url:page.ajaxUrl,action:"GetEmployees",index:1});
        $('#COORDINATEEMPLOYEE').newselectCombox({
            search: function (obj) { },
            cancel: function (obj) { if(obj.index==2){$('#COORDINATEEMPLOYEEID').val('');}else{$('#MAINEMPLOYEEID').val('');}},
            selected: function (val, text,record,obj){
                if(obj.index==2){
                    $('#COORDINATEEMPLOYEEID').val(val);
                }else{
                  $('#MAINEMPLOYEEID').val(val);
                }
                $('#txtCombox'+obj.index).textbox('setValue',text);
                $('#divComboxTxt'+obj.index).show();
                $('#divComboxSelect'+obj.index).hide(); 
            }
        },{},{url:page.ajaxUrl,action:"GetEmployees",index:2});
        
        //任务单信息
        $("#TransactionTable").datagrid({
            checkOnSelect: false,
            selectOnCheck: false,
            singleSelect: true,
            rownumbers: true,
            nowrap: false,
            onLoadSuccess: function (data) {
                $('#pp').pagination('refresh', {	// 改变选项，并刷新分页栏信息
                    total: page.pagetotal,
                    pageSize: page.pageSize,
                    pageNumber: page.pageNumber
                });
                customizedEasyuiPagination('pp'); //定制化分页
            },
            sortOrder:"desc",
            remoteSort:false,
		    multiSort:true,
            frozenColumns:[[
                { field: 'TRANSACTIONID', width: 120, checkbox: true },
                 { field: 'TRANSACTIONTYPESTATUS', title: '事务类型', width: 120,formatter:function(value,row,index){
                    var type=row.TRANSACTIONTYPE;
                    switch(type){
                        case "0":return "领导交办";
                        case "1":return "会议纪要";
                        case "2":return "其他";
                        case "3":return "领导班子会";
                        case "4":return "党会";
                        case "5":return "每周班子碰头会";
                        case "6":return "文件";
                        case "7":return "2023年重点工作";
                    }
                 } },
                 { field: 'TRANSACTIONCATEGORY',title:'事务类别',width:120,formatter:function(value,row,index){
                    switch(value){// //:技术、质量、生产交付AEOS、保密管理、成本财务、党建、行政、其他)
                        case "0":return "技术";
                        case "1":return "质量";
                        case "2":return "生产交付";
                        case "3":return "AEOS";
                        case "4":return "保密管理";
                        case "5":return "成本财务";
                        case "6":return "党建";
                        case "7":return "行政";
                        default:return "其他";
                    }
                 }},
                 { field: 'TRANSACTIONCONTENT', title: '事务内容', width: 400 },
            ]],
            columns: [[
                 { field: 'HANDLEITEM', title: '处理事项', width: 120 },
		         { field: 'MAINUNIT', title: '主责单位', width: 130 }, //--
                 { field: 'MAINEMPLOYEE', title: '主责人', width: 180 },
                 { field: 'COORDINATEUNIT', title: '配合单位', width: 120, },
                 { field: 'COORDINATEEMPLOYEE', title: '配合人', width: 80 },
                 { field: 'REQUIREDFINISHDATE', title: '要求完成时间', width: 100,sortable:true },
                 { field: 'STATUS', title: '事务状态', width: 60, },
                 { field: 'DELIVERABLES', title: '交付物', width: 100, align: 'center' },
                 { field: 'CREATEEMPLOYEE', title: '创建人', width: 80, },
                 { field: 'CREATEDATE', title: '创建时间', width: 150 ,sortable:true},
                 { field: 'RECIEVEEMPLOYEE', title: '接收人', width: 80,  },
                 { field: 'RECIEVEDATE', title: '接收时间', width: 150 },
                 { field: 'ACTUALFINISHDATE', title: '实际完成时间', width: 150 },
                 { field: 'XX', title: '', width: 20 }
	           ]],
            onSelect: function (index, row) {
                $('#EditorForm').form('load', row);
                $('#MAINEMPLOYEEID').combotree('setValues',row.MAINEMPLOYEEID.split(','));
                $('#MAINEMPLOYEEID').combotree('setText',row.MAINEMPLOYEE);
                $('#txtCombox2').textbox('setValue',row.COORDINATEEMPLOYEE);
                if(!$('#btnComboxSearch').hasClass('l-btn-disabled')){//防止连续行点击，查询按钮事件失效
                    $('#txtCombox').combobox('disable');
                    $('#btnComboxSearch').linkbutton('disable');
                }
                 $("#TransactionTable").datagrid("uncheckAll").datagrid("checkRow",index);
                
            },
             onCheckAll:function(rows){
                $("#TransactionTable").datagrid("clearSelections");
                $('#EditorForm').form("clear");
               
            },
            onCheck:function(rowIndex,rowData){
                var checkRows= $("#TransactionTable").datagrid("getChecked");
                var selectRow= $("#TransactionTable").datagrid("getSelected");
                if(checkRows.length>1){
                    $("#TransactionTable").datagrid("clearSelections");
                     $('#EditorForm').form("clear");
                }
            }
        });

        
        //分页事件
        $('#pp').pagination({
            onSelectPage: function (pageNumber, pageSize) {
                customizedEasyuiPagination('pp'); //定制化分页
                page.pageSize = pageSize; page.pageNumber = pageNumber;
                page.GetTransaction();
            }
        });
        //绑定分页go按钮
        $('#pageGo').on("click", function () {
            var pageNumber = $(this).parents('tr').find('.pagination-num').val();
            page.pageNumber = pageNumber;
            page.GetTransaction();
        });
        //清除按钮
        $('#btnReset').on("click", function () {
            page.reset(true, true);
            page.pageNumber = 1; page.pagetotal = 0
            $('#pp').pagination('refresh', {
                total: page.pagetotal,
                pageSize: page.pageSize,
                pageNumber: page.pageNumber
            });
        });

        $('#SumTable').datagrid({
            fitColumns:true,
            columns: [[
                 { field: 'tname', title: '分类',align:'center', width: 100 },
                 { field: 'finishcount', title: '已完成',align:'center', width: 100 },
                 { field: 'processcount', title: '进行中',align:'center', width: 100 },
                 { field: 'nofinishcount', title: '未完成',align:'center', width: 100 },
                 { field: 'total', title: '总计', align:'center',width: 100 }
                 ]],
                 onLoadSuccess:function(rowDatas)
                 {
                    var finishcount=0,processcount=0,nofinishcount=0,total=0;
                    $.each(rowDatas.rows,function(index,item){
                        finishcount+=Number(item.finishcount);
                        processcount+=Number(item.processcount);
                        nofinishcount+=Number(item.nofinishcount);
                        total+=Number(item.total);
                    });
                    $('#SumTable').datagrid('appendRow',{tname:'总计',finishcount:finishcount,processcount:processcount,nofinishcount:nofinishcount,total:total});
                 }
        
        });
    },
   
    //查询
    searchData: function () {
        page.reset(false, true);
        page.pageNumber = 1;
        page.GetTransaction();
        $('#EditorForm').form("clear");
    },
    //查询试车通知单信息
    GetTransaction: function () {
        var frdata = $('#searchForm').serializeArray();
        var data = { ZSTESTRUNRETURNBILLID: '' };
        $.each(frdata, function () {
            data[this.name] = this.value
        });
        var param = {
            action: "GetTransaction",
            pageSize: page.pageSize,
            pageNumber: page.pageNumber,
            data: JSON.stringify([data])
        };
        page.post(param, function (res) {
            if (res.Result) {
                //console.info(res);
                page.pagetotal = res.RowCount;
                page.mainEmplID=res.ReturnData;
                $('#TransactionTable').datagrid('loadData', res.DBTable);
            }
        });
    },
    //保存事务信息
    SaveTransaction: function () {
        var frdata = $('#EditorForm').serializeArray();
        var data = {};
        $.each(frdata, function () {
            data[this.name] = this.value
        });
        data.MAINEMPLOYEEID=$('#MAINEMPLOYEEID').combobox('getValues').join(',');
        var param = {
            action: "InsertTransaction",
            data: JSON.stringify([data])
        };
        if(data.STATUS!="待接收"&&data.STATUS!=""){displayMessage("事务"+data.STATUS+",不能再保存",false);return;}
        if (data.TRANSACTIONTYPE == "") { displayMessage("事务类型不能为空，请选择", false); return; }
        if (data.TRANSACTIONCATEGORY == "") { displayMessage("事务类别不能为空，请选择", false); return; }
        if (data.TRANSACTIONCONTENT == "") { displayMessage("事务内容不能为空，请输入", false); return; }
        if (data.HANDLEITEM == "") { displayMessage("处理事项不能为空，请输入", false); return; }
        if (data.REQUIREDFINISHDATE == "") { displayMessage("要求完成日期不能为空，请选择", false); return; }
        if (data.MAINUNIT == "") { displayMessage("主责单位不能为空，请输入", false); return; }
        if (data.MAINEMPLOYEEID == "") { displayMessage("责任人不能为空，请选择", false); return; }

        //if (data.COORDINATEUNIT == "") { displayMessage("配合单位不能为空，请输入", false); return; }
        //if (data.COORDINATEEMPLOYEEID == "") { displayMessage("配合人不能为空，请选择", false); return; }
        page.post(param, function (res) {
            if (res.Result) {
                page.GetTransaction();
                page.CreateTransaction();
            }
            displayMessage(res.Message, res.Result);
        });
    },
    //新建事务信息
    CreateTransaction: function () {
        $('#EditorForm').form("clear");
        $('#TransactionTable').datagrid('unselectAll');
    },
   
    //删除事务信息
    DeleteTransaction: function () {
        page.TransactionData("DeleteTransaction");
    },
    //接收事务信息
    RecieveTransaction: function () {
        page.TransactionData("RecieveTransaction");
    },
    FinishTransaction: function () {
        page.TransactionData("FinishTransaction");
    },
    NoFinishTransaction:function(){
        page.TransactionData("NoFinishTransaction");
    },
    //
    TransactionData: function (action) {
        var ids = [];
        var rows = $('#TransactionTable').datagrid("getChecked");
        $.each(rows, function (index, item) {
            if(action=="RecieveTransaction"&&page.pageType=="1"){
                if(item.MAINEMPLOYEEID.indexOf(page.mainEmplID)!=-1){
                    ids.push(item.TRANSACTIONID);
                }
            }else{
                ids.push(item.TRANSACTIONID);
            }
        });
        var param = {
            action: action,
            ids: ids.join(',')
        };
        if (ids.length == 0) { displayMessage(action=="RecieveTransaction"&&page.pageType=="1"?"请勾选【当前登录人作为主责人】记录再进行操作":"请勾选记录再进行操作", false); return; }

        if(page.pageType=="0"){
            if(action=="DeleteTransaction"&&rows[0].STATUS!="待接收"){displayMessage("所选事务记录"+rows[0].STATUS+",不能删除",false);return;}
        }
        if(page.pageType=="1"){
            if(action=="RecieveTransaction"&&rows[0].STATUS!="待接收"){displayMessage("所选事务记录"+rows[0].STATUS+",不能再接收",false);return;}
            if((action=="FinishTransaction"||action=="NoFinishTransaction")&&rows[0].STATUS!="进行中"){displayMessage("所选事务记录"+rows[0].STATUS+",不能再进行操作",false);return;}
        }

        page.post(param, function (res) {
            if (res.Result) {
                page.GetTransaction();
            }
            displayMessage(res.Message, res.Result);
        });
    },
    SumTransactionData:function(){
     $('#ddDialog').dialog({
            title: '统计查看',
            width: 950,
            height: 400,
            closed: false,
            modal: true
        });
     page.post({action:'SumTransaction'}, function (res) {
           $('#SumTable').datagrid('loadData',res.dbTable);
            var myChart = echarts.init(document.getElementById('main'));
            myChart.setOption({
                legend: {
                        orient: 'horizontal',
                        left: 'center',
                        top: 'bottom'
                      },
                series:[
                    {
                        name: '访问来源',
                        type: 'pie',    // 设置图表类型为饼图
                        radius: '55%',  // 饼图的半径，外半径为可视区尺寸（容器高宽中较小一项）的 55% 长度。
                        label: {
                        show: true,
                        formatter:function(param){
                          return param.name + ' (' + param.percent + '%)';
                        }
                },
                    data:res.returnData
                }
            ]
        });
     });
    },
   
    //查询重置
    searchReset: function () {
        $('#searchForm').form('clear');
        page.bind();
    },

    //清除方法，重要！！！
    reset: function (iscondition, isresult) {
        clearMessage();
    },
    //post数据
    post: function ( param, callback) {
        $.ajax({
            type: "Post",
            url: page.ajaxUrl,
            data: param,
            cashe: false,
            async: true,
            dataType: "json",
            beforeSend: function () {
                $.messager.progress({ text: '', msg: '处理中...' });
            },
            success: function (data) {
                $.messager.progress('close');
                callback(data);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $.messager.progress('close');
                displayMessage(XMLHttpRequest.responseText, false);
            }
        });
    },
    initComboxtree:function(){//
     var param = {action: "GetTreeData"};
        page.post(param, function (res) {
               $('#MAINEMPLOYEEID').combobox({
                        data: res.DBTable,
                        valueField: 'EMPLOYEEID',
                        textField: 'FULLNAME'
                    });
        });
//        $(function(){
//          $('#MAINEMPLOYEEID').selectUserCombox({
//            singleSelect: false,
//            panleHeight: '280px',
//            onChange: function (newvalue, oldvalue) {
//          
//            }});
//    });
    }
}
$(function () {
    page.init();
});